package com.example.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.reggie.dto.SetmealDto;
import com.example.reggie.entity.Setmeal;
import com.example.reggie.entity.SetmealDish;
import com.example.reggie.exception.CustomException;
import com.example.reggie.mapper.SetmealMapper;
import com.example.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    SetmealDishServiceImpl setmealDishService;

    /**
     * 新增套餐，同时需要保存套餐和菜品的关联关系
     *
     * @param setmealDto
     */
    @Transactional
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        this.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 修改套餐，同时需要保存套餐和菜品的关联关系
     * @param setmealDto
     */
    @Transactional
    public boolean updateWithFlavor(SetmealDto setmealDto) {
        //更改Setmeal
        this.updateById(setmealDto);
        //更改SetmealDto独有字段private
        // List<SetmealDish> setmealDishes
        // String categoryName
        //删除原先记录再添加增新纪录

        //查询在Setmeal_Dish表中对应的SetmealId,全部删除
        Long setmealId = setmealDto.getId();
        LambdaQueryWrapper<SetmealDish>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(setmealId!=null,SetmealDish::getSetmealId,setmealId);
        boolean remove = setmealDishService.remove(queryWrapper);
        if (!remove){
            throw new CustomException("套餐删除失败");
        }
        //原先的记录删除成功后
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes=setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        boolean b = setmealDishService.saveBatch(setmealDishes);
        if (!b){
            throw new CustomException("套餐新增失败");
        }
        return true;
    }

    /**
     * 批量/单个+停售/启售套餐
     * @param
     */
    public boolean updateWithStatus(String id, int status) {
        String[]arr=id.split(",");
        for (int i=0;i<arr.length;i++){
            long sid = Long.parseLong(arr[i]);
            Setmeal temp=new Setmeal();
            temp.setId(sid);
            temp.setStatus(status);
            boolean b = this.updateById(temp);
            if (!b){
                return false;
            }
        }
        return true;
    }

    /**
     * 批量/单个+删除套餐
     * @param
     */
    @Transactional
    public boolean delMulit(String ids) {
        String[] split = ids.split(",");
        for (int i=0;i< split.length;i++){
            Long id=Long.parseLong(split[i]);
            boolean b=this.removeById(id);
            LambdaQueryWrapper<SetmealDish>queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(id!=null,SetmealDish::getSetmealId,id);
            boolean a=setmealDishService.remove(queryWrapper);
            log.info("标志点:{} {}",b,a);
        }
        return true;
    }



}
